#include <windows.h>
#include <commctrl.h>
#include <strsafe.h>
#include "CTinyButton.hpp"

HWND g_myStatic;
HANDLE hOutput;
void consoleInit()
{
    AllocConsole();
    SetConsoleTitle(TEXT("Debug Window"));
    hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
}

HWND hStart;
HWND hStop;
CTinyButton tinyButton, tinyButton2;
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_CREATE:
    {
        // hStart = CreateWindow(WC_BUTTON, TEXT("start"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 110, 10, 100, 50, hWnd, NULL, NULL, NULL);
        // hStop = CreateWindow(WC_BUTTON, TEXT("stop"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 220, 10, 100, 50, hWnd, NULL, NULL, NULL);

        tinyButton.SetText(TEXT("start"));
        tinyButton.SetPosition(100, 50);
        tinyButton.Attach(hWnd);
        tinyButton.AddEvent(WM_LBUTTONDOWN, []()
                            {
            TCHAR buffer[256];
            StringCchPrintf(buffer, 256, TEXT("start WM_LBUTTONDOWN\n"));
            WriteConsole(hOutput, buffer, lstrlen(buffer), NULL, NULL); });
        tinyButton.AddEvent(WM_MOUSEMOVE, []()
                            {
            TCHAR buffer[256];
            StringCchPrintf(buffer, 256, TEXT("start WM_MOUSEMOVE\n"));
            WriteConsole(hOutput, buffer, lstrlen(buffer), NULL, NULL); });

        tinyButton2.SetText(TEXT("stop"));
        tinyButton2.SetPosition(300, 50);
        tinyButton2.Attach(hWnd);
        tinyButton2.SetRectRgn(30, 30);
        tinyButton2.AddEvent(WM_LBUTTONDOWN, []()
                             {
            TCHAR buffer[256];
            StringCchPrintf(buffer, 256, TEXT("stop WM_LBUTTONDOWN\n"));
            WriteConsole(hOutput, buffer, lstrlen(buffer), NULL, NULL); });
        tinyButton2.AddEvent(WM_LBUTTONUP, []()
                             {
            TCHAR buffer[256];
            StringCchPrintf(buffer, 256, TEXT("stop WM_LBUTTONUP\n"));
            WriteConsole(hOutput, buffer, lstrlen(buffer), NULL, NULL); });
    }
    break;
    case WM_INITDIALOG:
    {
    }
    break;
    case WM_PAINT:
    {
    }
    break;
    case WM_DRAWITEM:
    {
        LPDRAWITEMSTRUCT pDIS = (LPDRAWITEMSTRUCT)lParam;
        if (pDIS->hwndItem == g_myStatic)
        {
            SetTextColor(pDIS->hDC, RGB(100, 0, 100));
            TCHAR staticText[99];
            int len = SendMessage(g_myStatic, WM_GETTEXT,
                                  ARRAYSIZE(staticText), (LPARAM)staticText);
            TextOut(pDIS->hDC, pDIS->rcItem.left, pDIS->rcItem.top, staticText, len);
        }
    }
    break;
    case WM_SIZE:
    {
    }
    break;
    case WM_COMMAND:
    {
    }
    break;
    case WM_LBUTTONDOWN:
    {
        // int xPos = LOWORD(lParam);
        // int yPos = HIWORD(lParam);
        // TCHAR buffer[256];
        // StringCchPrintf(buffer, 256, TEXT("xPos: %d, yPos: %d"), xPos, yPos);
        // MessageBox(NULL, buffer, TEXT("WinMain"), MB_OK);
    }
    break;
    case WM_LBUTTONUP:
    {
        // int xPos = LOWORD(lParam);
        // int yPos = HIWORD(lParam);
        // TCHAR buffer[256];
        // StringCchPrintf(buffer, 256, TEXT("xPos: %d, yPos: %d"), xPos, yPos);
        // MessageBox(NULL, buffer, TEXT("WinMain"), MB_OK);
    }
    break;
    case WM_RBUTTONDOWN:
    {
        WriteConsole(hOutput, "WM_RBUTTONDOWN\n", 16, NULL, NULL);
    }
    break;
    case WM_KEYDOWN:
    {
    }
    break;
    case WM_QUERYENDSESSION:
    {
    }
    break;
    case WM_ENDSESSION:
    {
    }
    break;
    case WM_CLOSE:
    {
        DestroyWindow(hWnd);
    }
    break;
    case WM_DESTROY:
    {
        PostQuitMessage(0);
    }
    break;
    default:
    {
    }
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASS wc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WindowProc;
    wc.lpszClassName = TEXT("main");
    wc.lpszMenuName = NULL;
    wc.style = CS_HREDRAW | CS_VREDRAW;

    RegisterClass(&wc);

    consoleInit();
    HWND hWnd = CreateWindowEx(0, wc.lpszClassName, TEXT("Windows"), WS_OVERLAPPEDWINDOW,
                               CW_USEDEFAULT,
                               CW_USEDEFAULT,
                               CW_USEDEFAULT,
                               CW_USEDEFAULT,
                               NULL, NULL, hInstance, NULL);
    if (hWnd == NULL)
    {
        return 0;
    }
    // g_myStatic is a global HWND variable.
    g_myStatic = CreateWindowEx(0, TEXT("STATIC"), TEXT("Some static text"),
                                WS_CHILD | WS_VISIBLE | SS_OWNERDRAW,
                                25, 125, 150, 20, hWnd, 0, hInstance, 0);

    ShowWindow(hWnd, SW_SHOWNORMAL);
    UpdateWindow(hWnd);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}